#ifndef FLASH_H
#define FLASH_H

#include "string.h"
#include "stm32f4xx.h"
#include "include.h"


/**************************************************************************
| 0x08000000 | 0x08010000 |  0x08020000   |  0x08040000   |  0x08060000   |
---------------------------------------------------------------------------
|    BOOT    |  SETTING   |      APP1     |      APP2     |      APP3     |
---------------------------------------------------------------------------
|    64K     |     64K    |      128K     |      128K     |      128K     |
***************************************************************************/

/*
APP1 : 工作区

APP2 : 暂存区

APP3 : 备份区
*/

typedef enum
{
  Read_OK = 0,
  Addr_Error,
  Erase_Error,
  Write_Error

}FLASH_READ_STATUS;

#define FLASH_SECTOR_SIZE           1024
#define FLASH_SECTOR_NUM            512    // 512K
#define FLASH_START_ADDR            ((uint32_t)0x8000000)
#define FLASH_END_ADDR              ((uint32_t)(0x8000000 + FLASH_SECTOR_NUM * FLASH_SECTOR_SIZE))
//flash sector addr
#define ADDR_FLASH_SECTOR_0         ((uint32_t)0x08000000) 	//sector0 addr, 16 Kbytes
#define ADDR_FLASH_SECTOR_1         ((uint32_t)0x08004000) 	//sector1 addr, 16 Kbytes
#define ADDR_FLASH_SECTOR_2         ((uint32_t)0x08008000) 	//sector2 addr, 16 Kbytes
#define ADDR_FLASH_SECTOR_3         ((uint32_t)0x0800C000) 	//sector3 addr, 16 Kbytes
#define ADDR_FLASH_SECTOR_4         ((uint32_t)0x08010000) 	//sector4 addr, 64 Kbytes
#define ADDR_FLASH_SECTOR_5         ((uint32_t)0x08020000) 	//sector5 addr, 128 Kbytes
#define ADDR_FLASH_SECTOR_6         ((uint32_t)0x08040000) 	//sector6 addr, 128 Kbytes
#define ADDR_FLASH_SECTOR_7         ((uint32_t)0x08060000) 	//sector7 addr, 128 Kbytes
#define ADDR_FLASH_SECTOR_8         ((uint32_t)0x08080000) 	//sector8 addr, 128 Kbytes      //no

#define BOOT_SECTOR_ADDR            ADDR_FLASH_SECTOR_0         // BOOT sector start addres
#define BOOT_SECTOR_SIZE            0x10000                     // BOOT sector size
#define SETTING_SECTOR_ADDR         ADDR_FLASH_SECTOR_4         // SETTING sector start addres
#define SETTING_SECTOR_SIZE         0x10000                     // SETTING sector size
#define APP_SECTOR_ADDR             ADDR_FLASH_SECTOR_5         // APP sector start address
#define APP_SECTOR_SIZE             0x20000                     // APP sector size


#define FLASH_WAITETIME             50000          //FLASH等待超时时间


uint32_t STM32_FLASH_ReadWord(uint32_t faddr);
uint8_t mcu_flash_write(uint32_t addr, uint8_t *buffer, uint32_t length);
void mcu_flash_read(uint32_t addr, uint8_t *buffer, uint32_t length);
uint8_t get_process_status(void);
void copy_flash_bin_128kb( uint32_t sources_bin, uint32_t target_bin );
#endif